from django.shortcuts import render
from .models import Post


# Create your views here.

def index(request):
    post_list = Post.objects.all().order_by('-created_time')
    return render(request, 'blog/index.html', context={'post_list': post_list})


# user_list = []
#
# def login(request):
#     if request.method == "POST":
#         global user_list # 没有写这段代码前，一直报变量未定义错误。
#         username = request.POST.get("username", None)
#         password = request.POST.get("password", None)
#         # temp = {"user": username, "pwd": password}
#         # user_list.append(temp)
#        # print(username, password)
#        # models.UserInfo.objects.all().delete()
#         models.UserInfo.objects.create(user=username, pwd=password)
#         user_list = models.UserInfo.objects.all()
#     return render(request, "login.html", {"data": user_list})
#
# # def main(request):
# #     if request.method == "POST":
# #         username = request.POST.get("username", None)
# #         password = request.POST.get("password", None)
# #         temp = {"user":username,"pwd":password}
# #         user_list.append(temp)
# #         print(username, password)
# #     return render(request, "main.html",{"data":user_list})
#
# def publish(request):
#     ret = {'status': False, 'data': '', 'error': '', 'summary': ''}
#     if request.method == 'POST':
#         request_form =form.PublishForm(request.POST)
#         if request_form.is_valid():# 取回来的数据进行合法验证
#             request_dict = request_form.clean()
#             print (request_dict)
#             ret['status'] = True
#         else:
#             error_msg = request_form.errors.as_json()
#             ret['error'] = json.loads(error_msg)
#     return HttpResponse(json.dumps(ret))
#
#
# from django.template.context import RequestContext
#
#
# # Create your views here.
#
#
# def test(request):
#     if request.method == 'POST':
#         return HttpResponse('ok')
#     return render('app01/test.html', context_instance=RequestContext(request))


# def archive(request):
#
#     posts = BlogPost.objects.all()
#     t = loader.get_template('archive.html')
#     c = Context({'posts':posts})
#     return HttpResponse(t.render(c))
